﻿using System;
using System.Collections.Generic;
using System.Text;
using FJASPX.Framwork.Constant;
using FJASPX.Framwork.Helper;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;
using NHibernate.Engine;
using NHibernate.SqlCommand;
using NHibernate.SqlTypes;

namespace FJASPX.Framwork.Config
{
    public static class NHI
    {
        #region Properties属性

        private static readonly object _lockObject = new object();

        private static ISessionFactory m_factory;

        public static ISessionFactory Factory
        {
            get
            {
                if (m_factory == null)
                {
                    lock (_lockObject)
                    {
                        if (m_factory == null)
                        {
                            try
                            {
                                NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration().Configure(Cfg.NHI_CONFIG);
                                m_factory = cfg.BuildSessionFactory();
                            }
                            catch (Exception ex)
                            {
                                Log.WriteLog(Message.ERR, ex);
                            }
                        }
                    }
                }
                return m_factory;
            }
        }

        #endregion

        #region Methods方法

        /// <summary>
        /// 初始化NHibernate
        /// </summary>
        public static void InitFactory()
        {
            lock (_lockObject)
            {
                NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration().Configure(Cfg.NHI_CONFIG);
                m_factory = cfg.BuildSessionFactory();
            }
        }

        #endregion
    }
}
